package com.yzt.zhuju.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Build;
import android.text.TextUtils;
import android.util.Log;

import com.yzt.p_base.BuildConfig;
import com.yzt.p_base.utils.SharedPreferenceUtil;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.UUID;

/**
 * author : bob
 * date   : 2022/11/29
 * desc   : 获取设备工具类
 */
public class DeviceUtils {

    private static String uuid;
    private static String brand;
    private static String model;
    private static String sysVersion;

    private DeviceUtils() {
        throw new UnsupportedOperationException("u can't instantiate me...");
    }

    /**
     * 获取系统版本字符串 (4.1.2 或7.1.2等)
     *
     * @return
     */
    public static String getSysVersion() {
        if (TextUtils.isEmpty(sysVersion)) {
            String version = SharedPreferenceUtil.getInstance().getValue("SysVersion", "");
            if (TextUtils.isEmpty(version)) {
                sysVersion = android.os.Build.VERSION.RELEASE;
                SharedPreferenceUtil.getInstance().putValue("SysVersion", sysVersion);
            } else {
                sysVersion = version;
            }
        }
        return sysVersion;
    }


    /**
     * 获取设备AndroidID
     *
     * @param context 上下文
     * @return AndroidID
     */
    @SuppressLint("HardwareIds")
    public static String getAndroidID(Context context) {
        // return Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
        // 现在不允许手机android id了，所以返回uuid
        return getUUID();
    }

    /**
     * 获取设备品牌
     */
    public static String getDeviceBrand() {
        if (TextUtils.isEmpty(brand)) {
            String deviceBrand = SharedPreferenceUtil.getInstance().getValue("DeviceBrand", "");
            if (TextUtils.isEmpty(deviceBrand)) {
                brand = Build.BRAND;
                SharedPreferenceUtil.getInstance().putValue("DeviceBrand", brand);
            } else {
                brand = deviceBrand;
            }
        }
        return brand;
    }

    /**
     * 获取手机型号
     */
    public static String getSystemModel() {
        if (TextUtils.isEmpty(model)) {
            String systemModel = SharedPreferenceUtil.getInstance().getValue("SystemModel", "");
            if (TextUtils.isEmpty(systemModel)) {
                model = android.os.Build.MODEL;
                SharedPreferenceUtil.getInstance().putValue("SystemModel", model);
            } else {
                model = systemModel;
            }
        }
        return model;
    }

    /**
     * 获取android唯一识别码
     */
    public static String getDeviceUuid(Context context) {
        if (TextUtils.isEmpty(uuid)) {
            String cacheDeviceId = SharedPreferenceUtil.getInstance().getValue("cacheDeviceId", "");
            if (!TextUtils.isEmpty(cacheDeviceId)) {
                uuid = cacheDeviceId;
                return cacheDeviceId;
            }
            Boolean isClickPrivateDialogYes = SharedPreferenceUtil.getInstance().getValue("isClickPrivateDialogYes", false);
            if (isClickPrivateDialogYes) {
                String androidId = getAndroidID(context);
                Log.d("androidIds-1", androidId);
                //dd222616dd08a002
                if (!"".equals(androidId) || !BuildConfig.DEBUG) {
                    uuid = UUID.nameUUIDFromBytes((androidId).getBytes(StandardCharsets.UTF_8)).toString();
                    SharedPreferenceUtil.getInstance().putValue("cacheDeviceId", uuid);
                    Log.d("androidIds-2", uuid + "");
                } else {
                    uuid = getUUID();
                    SharedPreferenceUtil.getInstance().putValue("cacheDeviceId", uuid);
                    Log.d("androidIds-3", uuid + "");
                }
            } else {
                uuid = getUUID();
            }
        }
        return uuid;
    }

    private static String getUUID() {
        String cacheDeviceId = SharedPreferenceUtil.getInstance().getValue("cacheDeviceId", "");
        if (TextUtils.isEmpty(cacheDeviceId)) {
            String uuid = UUID.randomUUID().toString();
            SharedPreferenceUtil.getInstance().putValue("cacheDeviceId", uuid);
            return uuid;
        } else {
            return cacheDeviceId;
        }
    }
}
